/* It gives first derivative at the points "xderv". It is not necessary that these points
   must lie within the points "x". Instead, these can be "anywhere". */

# include "scene.h"
# include <stdlib.h>

extern short cmtrx_CD(double *x, unsigned short nxpt, double *xderv, unsigned short nxdervpt,\
 unsigned short ndiscr, double **mtrx);

extern short cmtrx_make(double **mtrx, unsigned short nrows, unsigned short ncols, struct cmtrx *cm);

extern short cmtrx_1stderv_CD(double *x, unsigned short nxpt, double *xderv, unsigned short nxdervpt,\
 unsigned short ndiscr, struct cmtrx *cm, double **tempmtrx)
 {
  double **mtrx;
  unsigned short i;
  short ecode = 1;

  if(tempmtrx != (double **)0) mtrx = tempmtrx;
  else{
       mtrx = (double **)malloc(sizeof(double *)*nxdervpt);
       for(i = 0; i < nxdervpt; i++) mtrx[i] = (double *)malloc(sizeof(double)*nxpt);
      }

  ecode = cmtrx_CD(x, nxpt, xderv, nxdervpt, ndiscr, mtrx);

  if(ecode > 0) ecode = cmtrx_make(mtrx, nxdervpt, nxpt, cm);

  if(tempmtrx == (double **)0)
     {
      for(i = 0; i < nxdervpt; i++) free(mtrx[i]);
      free(mtrx);
     }

  return ecode;
 }
